<?php

namespace App\Exceptions;

use App\Traits\ResponseTrait;
use Arr;
use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\Exceptions\ThrottleRequestsException;
use Illuminate\Validation\ValidationException;
use Spatie\Permission\Exceptions\UnauthorizedException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
use Tymon\JWTAuth\Exceptions\TokenInvalidException;

class Handler extends ExceptionHandler
{

    use ResponseTrait;

    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param  \Exception  $exception
     * @return void
     */
    public function report(Exception $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Exception $exception
     * @return \Illuminate\Http\JsonResponse
     * @throws Exception
     */
    public function render($request, Exception $exception)
    {
        if ($exception instanceof NotFoundHttpException) {
            return $this->failure($exception->getMessage(), '', Response::HTTP_INTERNAL_SERVER_ERROR);
        }

        if ($exception instanceof TokenInvalidException) {
            return $this->failure($exception->getMessage(), '', 50008);
        }

        if ($exception instanceof TokenExpiredException) {
            return $this->failure($exception->getMessage(), '', 50014);
        }

        if ($exception instanceof ModelNotFoundException) {
            return $this->failure($exception->getMessage(), '', Response::HTTP_INTERNAL_SERVER_ERROR);
        }
        // 参数验证错误的异常，我们需要返回 422 的 http code 和一句错误信息
        if ($exception instanceof ValidationException) {
            return $this->failure(Arr::first(Arr::collapse($exception->errors())), '', Response::HTTP_UNPROCESSABLE_ENTITY);
        }
        // 用户认证的异常，我们需要返回 401 的 http code 和错误信息
        if ($exception instanceof UnauthorizedHttpException) {
            return $this->failure($exception->getMessage(), '', Response::HTTP_UNAUTHORIZED);
        }
        // 请求太频繁
        if ($exception instanceof ThrottleRequestsException) {
            return $this->failure($exception->getMessage(), '', Response::HTTP_TOO_MANY_REQUESTS);
        }
        // 没有权限
        if ($exception instanceof UnauthorizedException) {
            return $this->failure($exception->getMessage(), '', Response::HTTP_FORBIDDEN);
        }

        return parent::render($request, $exception);
    }
}
